package com.prs.crm.action.oa;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.doc.Document;
import com.prs.crm.domain.oa.OaTaskKeyWord;
import com.prs.crm.service.oa.OaTaskKeyWordService;

public class OaTaskKeyWordAction extends BaseAction {

	private static final long serialVersionUID = 8689785016656521988L;

	private OaTaskKeyWord oaTaskKeyWord;

	private OaTaskKeyWordService oaTaskKeyWordService;

	private PaginationSupport pagination;

	private Integer[] oaTaskKeyWordIds;


	@Action("listOaTaskKeyWord")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				OaTaskKeyWord.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOaTaskKeyWordService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@SuppressWarnings("unchecked")
	@Action("listKeyTask")
	public String execute1() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Document.class);
        OaTaskKeyWord  OaTaskKeyWordch=this.getOaTaskKeyWordService().getObjectOaTaskKeyWord(oaTaskKeyWord.getId());
        Set<Document> s=OaTaskKeyWordch.getDocuments();
        List<Document> list = new ArrayList<Document>(s);  

        if(s.size()==0){
        	 criteria.add(Restrictions.eq("id",0));
        }else{
        	for (int i=0;i<list.size();i++) {
           Document po=list.get(i);   
              criteria.add(Restrictions.or(Restrictions.eq("name",po.getName()),Restrictions.eq("name",po.getName())));
           }
        }
  
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOaTaskKeyWordService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectOaTaskKeyWord")
	public String selectKnowledgeType() {
		return this.execute();
	}

	@Action("editOaTaskKeyWord")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveOaTaskKeyWord", results = { 
			@Result(name = "success", type = "redirect", location = "listOaTaskKeyWord") ,
			@Result(name = "error", type = "chain", location = "editOaTaskKeyWord")})
	public String save() {
		int Rep=this.getOaTaskKeyWordService().serchRep(oaTaskKeyWord.getKeyName());
		if(Rep==0){
			this.getOaTaskKeyWordService().save(oaTaskKeyWord);
			return SUCCESS;
		}else{
			this.addActionError("关键字名字重复");
			return ERROR;
		}
		
	}

	@Action(value = "removeOaTaskKeyWord", results = {
			@Result(name = "success", type = "redirect", location = "listOaTaskKeyWord"),
			@Result(name = "error", type = "chain", location = "listKnowledgeType") })
	public String remove() {
		if (oaTaskKeyWordIds == null) {
			this.addActionError("请选择要删除的关键字");
			return ERROR;
		}
		this.getOaTaskKeyWordService().batchRemove(oaTaskKeyWordIds);
		return SUCCESS;
	}

	public Integer[] getOaTaskKeyWordIds() {
		return oaTaskKeyWordIds;
	}

	public void setOaTaskKeyWordIds(Integer[] oaTaskKeyWordIds) {
		this.oaTaskKeyWordIds = oaTaskKeyWordIds;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public OaTaskKeyWord getOaTaskKeyWord() {
		return oaTaskKeyWord;
	}

	public void setOaTaskKeyWord(OaTaskKeyWord oaTaskKeyWord) {
		this.oaTaskKeyWord = oaTaskKeyWord;
	}

	public OaTaskKeyWordService getOaTaskKeyWordService() {
		return oaTaskKeyWordService;
	}

	public void setOaTaskKeyWordService(
			OaTaskKeyWordService oaTaskKeyWordService) {
		this.oaTaskKeyWordService = oaTaskKeyWordService;
	}

}
